package com.heatingcharge.service;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Lists;
import com.google.common.collect.Multimap;
import com.heatingcharge.dto.DeptLevelDTO;
import com.heatingcharge.entity.sys.SysDept;
import com.heatingcharge.mapper.sys.SysDeptMapper;
import com.heatingcharge.common.util.LevelUtil;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

@Service
public class SysTreeService {

    @Autowired
    SysDeptMapper sysDeptMapper;

    /**
     * 获取部门信息
     * @return
     */
    public List<DeptLevelDTO> getDept(){
        List<SysDept> deptList=sysDeptMapper.selectDepts();
        List<DeptLevelDTO> dtoList= Lists.newArrayList();
        for (SysDept dept:deptList){
            DeptLevelDTO dto= DeptLevelDTO.adapt(dept);
            dtoList.add(dto);
        }
        return deptDtoListToTree(dtoList);
    }
    private List<DeptLevelDTO> deptDtoListToTree(List<DeptLevelDTO> dtoList) {
        if (CollectionUtils.isEmpty(dtoList)){
            return Lists.newArrayList();
        }
        Multimap<String,DeptLevelDTO> levelDeptMap= ArrayListMultimap.create();
        List<DeptLevelDTO> rootList=Lists.newArrayList();
        for (DeptLevelDTO dto:dtoList){
            levelDeptMap.put(dto.getLevel(),dto);
            if (LevelUtil.ROOT.equals(dto.getLevel())){
                rootList.add(dto);
            }
        }
        // 按照seq从小到大排序
        Collections.sort(rootList, new Comparator<DeptLevelDTO>() {
            public int compare(DeptLevelDTO o1, DeptLevelDTO o2) {
                return o1.getSeq() - o2.getSeq();
            }
        });
        // 递归生成树
        transformDeptTree(rootList, LevelUtil.ROOT, levelDeptMap);
        return rootList;
    }

    /**
     * 递归部门树
     * @param deptLevelList
     * @param level
     * @param levelDeptMap
     */
    private void transformDeptTree(List<DeptLevelDTO> deptLevelList, String level, Multimap<String, DeptLevelDTO> levelDeptMap) {
        for (int i = 0; i < deptLevelList.size(); i++) {
            // 遍历该层的每个元素
            DeptLevelDTO deptLevelDto = deptLevelList.get(i);
            // 处理当前层级的数据
            String nextLevel = LevelUtil.calculateLevel(level, deptLevelDto.getId());
            // 处理下一层
            List<DeptLevelDTO> tempDeptList = (List<DeptLevelDTO>) levelDeptMap.get(nextLevel);
            if (CollectionUtils.isNotEmpty(tempDeptList)) {
                // 排序
                Collections.sort(tempDeptList, deptSeqComparator);
                // 设置下一层部门
                deptLevelDto.setChildren(tempDeptList);
                // 进入到下一层处理
                transformDeptTree(tempDeptList, nextLevel, levelDeptMap);
            }
        }
    }

    public Comparator<DeptLevelDTO> deptSeqComparator = new Comparator<DeptLevelDTO>() {
        public int compare(DeptLevelDTO o1, DeptLevelDTO o2) {
            return o1.getSeq() - o2.getSeq();
        }
    };
}
